/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package models;

import cstAndEnum.*;
import java.util.ArrayList;
import java.util.List;

/**
 *玩家列表
 * @author Invince
 */
public class ListPlayer {
    /*----------Attributs-------------------*/
    /**
     * 玩家列表
     */
    protected List<Player> list;
    /*----------Public functions------------*/
    /**
     * 加入一个玩家，如果已有同名玩家返回false
     * @param player
     * @return
     */
    public boolean addPlayer(Player player){
        if(!list.contains(player)){
            String r="ListPlayer--addPlayer: "+player.getName();
            CST.println(r);
            return list.add(player);//Always true here
        }
        String r="Failed: ListPlayer--addPlayer:"+player.getName()+" failed, cause this player exists already";
        CST.println(r);
        return false;
    }

    /**
     * 通过名字寻找玩家
     * @param name
     * @return
     */
    public Player findPlayerByName(String name){
        for(int i=0;i<list.size();i++){
            if(list.get(i).getName().equals(name)){
                return list.get(i);
            }
        }
        return null;
    }

    /**
     * 计算出两玩家之间的距离，注意有进攻防守之分（因为自动考虑了马的效果）
     * @param attacker
     * @param defender
     * @return
     */
    public int distanceBetween(Player attacker,Player defender){
        int p1=list.indexOf(attacker);
        int p2=list.indexOf(defender);
        if(p1==-1 || p2==-1){//说明不在列表里
            CST.println("ListPlayer--distanceBetween : error attacker or defender not in the listPlayer");
            return -1;
        }
        int dis=Math.abs(p2-p1);
        if(dis>list.size()/2){//如果顺时针距离大于总玩家数一半，说明逆时针方向的距离比较小，所以dis应该取逆时针方向
            dis=list.size()-dis;
        }
        if(attacker.getDeckEqp().getHorseMinus()!=null){
            dis--;
        }
        if(defender.getDeckEqp().getHorsePlus()!=null){
            dis++;
        }
        String r="ListPlayer--distanceBetween : "+attacker.getName()+" and " +defender.getName() +" is "+dis;
        CST.println(r);
        return dis;
    }

    /**
     * 返回玩家数
     * @return
     */
    public int size(){
        return this.list.size();
    }

    /**
     * 返回第index个玩家
     * @param index
     * @return
     */
    public Player get(int index){
        return this.list.get(index);
    }

    /*----------Private functions-----------*/
    /*----------Constructor-----------------*/
    /**
     * Constructor
     * @param list
     */
    public ListPlayer(List<Player> list) {
        this.list=new ArrayList<Player>();
        for(int i=0;i<list.size();i++){
            this.addPlayer(list.get(i));
        }
    }

    /**
     * Constructor void
     */
    public ListPlayer() {
        this.list=new ArrayList<Player>();
    }



    /*----------Gets and sets---------------*/
    public List<Player> getList() {
        return list;
    }

    public void setList(List<Player> list) {
        this.list = list;
    }
}
